// pages/byYear/index.js
import { formatDate } from '../../utils/util'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isExpense: 'true',
    isLoad: false,

    allData: [],

    searchDt: '',
    searchStartDt: '',
    searchEndDt: '',
    findList: [],
    totalMoney: 0,
    recordCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isExpense: options.isExpense || 'true',
      searchDt: new Date().getFullYear()
    })
    this.loadData()
  },

  onShow: function () {
  },
  loadData(year) {
    this.setData({
      isLoad: true,
      searchDt: year || this.data.searchDt
    })
    app.post({
      action: 'getExpenseListByYear',
      data: {
        isExpense: this.data.isExpense,
        year: this.data.searchDt + ''
      }
    }).then(res => {
      let sum = 0
      res.data.forEach(item => {
        item.money = (+item.money).toFixed(2)
        sum += +item.money
      })
      console.log(res.data)
      this.setData({
        isLoad: false,
        allData: res.data,
        findList: res.data,
        totalMoney: sum.toFixed(2)
      })
    }).catch(res => {
      this.setData({
        isLoad: false,
        allData: [],
        findList: [],
        totalMoney: 0
      })
    })
  },
  preYear: function (e) {
    this.loadData(this.data.searchDt - 1)
  },
  nextYear: function (e) {
    this.loadData(this.data.searchDt + 1)
  },
  searchDateChange: function (e) {
    this.loadData(e.detail.value)
  },
  monthDetail: function (e) {
    let date = e.currentTarget.dataset.date
    date = date.split('-').map(item => {
      return parseInt(item)
    })

    wx.navigateTo({
      url: `/pages/byMonth/index?isExpense=${this.data.isExpense}&year=${date[0]}&month=${date[1] - 1}`,
    })
  }
})